package com.cmdandroid.icebutton;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



public class ICEButtonManageContactsActivity extends ICEButtonActivity{
	
	public ArrayList<String> names = new ArrayList<String>();
	public ArrayList<String> numbers = new ArrayList<String>();

	public LinearLayout canvas;
	
	@Override
	
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.manage_contacts);

		
		//initialize canvas
		 canvas = new LinearLayout(this);
		 try {
			 Bundle extras = getIntent().getExtras();
			 ArrayList<String> infoFromActivity = new ArrayList<String>();
			 
			 infoFromActivity = extras.getStringArrayList(CONTACTDATAKEY_INFO);
			 names.clear();
			 numbers.clear();
			 
			 for (int i=0; i<infoFromActivity.size(); i=i+2) {
				 names.add(infoFromActivity.get(i));
				 numbers.add(infoFromActivity.get(i+1));
			 }
			 
		 }
		 catch (NullPointerException e) {}
		 
		 
		 // Debug start
		 // names.add("Albert Einstein"); names.add("Billy Bronco"); names.add("Cal Poly");
		 // numbers.add("111-111-1111"); numbers.add("222-222-2222"); numbers.add("333-333-3333");
		 // Debug end
		 
		 initGUI();
	}
	
	public void initGUI() {
		
		//Init save and cancel buttons
		Button saveButton = (Button) findViewById(R.id.button_manage_contacts_Save);
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent result = new Intent();
				Bundle b = new Bundle();
				ArrayList<String> info = new ArrayList<String>();
				
				//Format info for {name1, number1, name2, number2,...}
				for (int i=0; i<names.size(); i++) {
					info.add(names.get(i));
					info.add(numbers.get(i));
				}
				
				b.putStringArrayList(CONTACTDATAKEY_INFO, info);
				result.putExtras(b);
				
				setResult(Activity.RESULT_OK, result);
				
				finish();
				
			}
		});
		
		//Add addButton and associated actions
		
		Button cancelButton = (Button) findViewById(R.id.button_manage_contacts_Cancel);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent result = new Intent();
				setResult(Activity.RESULT_CANCELED, result);
				finish();
			}
		});
		
		
		
		//Init dynamic elements
		//clear canvas
		canvas.removeAllViews();
		LayoutInflater inflater;
		inflater = LayoutInflater.from(getApplicationContext());

		final List<View> childView = new ArrayList<View>();
		final List<Button> childButtons = new ArrayList<Button>();
		
		final List<TextView> childTextViewsNames = new ArrayList<TextView>(); //TextView for Names
		final List<TextView> childTextViewsNumbers = new ArrayList<TextView>(); //TextView for Numbers

		canvas = (LinearLayout) 
			ICEButtonManageContactsActivity.this.findViewById(
					R.id.LinearLayout_manage_contacts_root);
		
		for (char i = 0; i<names.size(); i++) {
		childView.add(inflater.inflate(R.layout.manage_contacts_element, canvas, false));
		canvas.addView(childView.get(i));
		}
		
		for (char i=0; i<childView.size(); i++) {
			
		childTextViewsNames.add((TextView) childView.get(i).
				findViewById(R.id.textView_manage_contacts_name));
		childTextViewsNames.get(i).setText(names.get(i).toString());
		
		childTextViewsNumbers.add((TextView) childView.get(i).
				findViewById(R.id.textView_manage_contacts_number));
		childTextViewsNumbers.get(i).setText(numbers.get(i).toString());
		
			
		childButtons.add((Button) childView.get(i).
				findViewById(R.id.button_manage_contacts_delete)); 
		childButtons.get(i).setId(i);
		childButtons.get(i).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Get id of button clicked
				Integer id = ((Button)v).getId();
				
				//TODO: Remove for debug
				Toast.makeText(getApplicationContext(),
						"You clicked button " + id, Toast.LENGTH_SHORT).show();
				
				//Remove values for item clicked
				numbers.remove((int)id);
				names.remove((int)id);
				//Remove all views from canvas to prepare for rebuild
				//Removed because added in initGUI()
				//canvas.removeAllViews();
				
				//rebuild GUI with new numbers and names
				initGUI();
				
			}
		});
		}
	}
}
